package kr.co.graviti.controller.login;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.common.util.CommonVariable;
import kr.co.graviti.service.login.LoginService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Administrator
 * 관리자 로그인폼, 로그인처리
 */
@Controller
public class LoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginService loginService;
	
	/**
	 * 관리자 로그인 처리
	 * @param request
	 * @param rseponse
	 * @return ModelAndView
	 */
	@RequestMapping(value="/login/loginForm", method=RequestMethod.GET)
	public ModelAndView loginForm(HttpServletRequest request,
			HttpServletResponse response){
		
		return new ModelAndView("login/loginForm");
	}
	
	/**
	 * 로그인 처리 (메뉴권한 세션 생성)
	 * @param request
	 * @param response
	 * @param userid
	 * @param passwd
	 * @return ModelAndView
	 */
	@RequestMapping(value="/login/loginProcess")
	public ModelAndView loginProcess(HttpServletRequest request
			, HttpServletResponse response
			, @RequestParam(value="userid") String userid
			, @RequestParam(value="passwd") String passwd
			, @RequestParam(value="lang") String lang){
		
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("userid", userid);
		param.put("passwd", passwd);
		param.put("lang", lang);
		
		HashMap<String, Object> result = loginService.selectAdminCount(param);
		
		if(result != null){		//	로그인 성공
			result.put("lang", lang);
			List<HashMap<String, Object>> topMenuList = loginService.selectTopMenuAuthority(result);
			List<HashMap<String, Object>> leftMenuList = loginService.selectLeftMenuAuthority(result);
			
			this.setSession(result, topMenuList, leftMenuList, request);	//세션 생성
		}
		
		logger.info("result : " + result);
		logger.info("lang : " + lang);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName(new String("login/bin"));
		mav.addObject("result", result);
		
		return mav;
	}
	
	public void setSession(HashMap<String, Object> result
			, List<HashMap<String, Object>> topMenuList
			, List<HashMap<String, Object>> leftMenuList
			, HttpServletRequest request){
		
		request.getSession().setAttribute(CommonVariable.SESSION_TOP_MENU
				, topMenuList);
		request.getSession().setAttribute(CommonVariable.SESSION_LEFT_MENU
				, leftMenuList);
		request.getSession().setAttribute(CommonVariable.SESSION_ADMIN_ID
				, result.get("userid")); //이름, 메뉴리스트 추가해야 함
		request.getSession().setAttribute(CommonVariable.SESSION_ADMIN_NM
				, result.get("usernm")); //이름, 메뉴리스트 추가해야 함
		request.getSession().setAttribute(CommonVariable.SESSION_ADMIN_SOSOK
				, result.get("sosok")); //이름, 메뉴리스트 추가해야 함
		request.getSession().setAttribute(CommonVariable.SESSION_LANG
				, result.get("lang")); //이름, 메뉴리스트 추가해야 함
		
		logger.info("daumcorp_admin_userid : " + request.getSession()
				.getAttribute(CommonVariable.SESSION_ADMIN_ID));
	}
			
	/**
	 * 로그인 성공 후 메인페이지 이동
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/main/list", method=RequestMethod.GET)
	public ModelAndView main(HttpServletRequest request
			, HttpServletResponse response) {
		
		return new ModelAndView(new String("main/main"));
	}
	
	@RequestMapping(value="/main/main", method=RequestMethod.GET)
	public ModelAndView form(HttpServletRequest request
			, HttpServletResponse response) {
		
		return new ModelAndView(new String("main/main"));
	}
	
	
	/**
	 * 로그아웃 처리 후 로그인페이지로 이동
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/login/logout", method=RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request
			, HttpServletResponse response) {
		
		request.getSession().removeAttribute(CommonVariable.SESSION_ADMIN_ID);
		
		return new ModelAndView(new String("login/loginForm"));
	}
}
